package com.bjsdzk.collector.rpc;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjsdzk.backend.rpc.collector.BaseLocalDeviceCollect;
import com.bjsdzk.backend.rpc.entity.collector.DeviceCmdInfo;
import com.bjsdzk.backend.rpc.entity.collector.DeviceInfo;
import com.bjsdzk.backend.rpc.entity.response.Result;
import com.bjsdzk.backend.rpc.entity.response.ResultHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author pc
 */
@Service(
        version = "1.0.0",
        interfaceClass = BaseLocalDeviceCollect.class
)
@Component
@Slf4j
public class LocalDeviceServiceImp implements BaseLocalDeviceCollect {

    private Map<String, BaseLocalDeviceCollect> localDeviceServiceMap;

    @Autowired
    public void setOrderHandleMap(List<BaseLocalDeviceCollect> orderHandlers) {
//        orderHandleMap = orderHandlers.stream().collect(
//                Collectors.toMap(orderHandler -> AnnotationUtils.findAnnotation(orderHandler.getClass(), BaseLocalDeviceCollectType.class).dtuType(),
//                        v -> v, (v1, v2) -> v1));

        localDeviceServiceMap = orderHandlers.stream().flatMap(localCommunicationService -> {
            String[] dtuTypes = AnnotationUtils.findAnnotation(localCommunicationService.getClass(), LocalCommunicationServiceType.class).dtuType();
            LocalDeviceContrast[] localServices = new LocalDeviceContrast[dtuTypes.length];
            for (int i = 0; i < dtuTypes.length; i++) {
                localServices[i] = new LocalDeviceContrast(dtuTypes[i], localCommunicationService);
            }
            return Arrays.stream(localServices);

        }).collect(Collectors.toMap(LocalDeviceContrast::getDtuType,
                LocalDeviceContrast::getBaseLocalDeviceCollect, (v1, v2) -> v1));
    }
    @Override
    public Result deviceAdd(DeviceInfo deviceInfo) {
        log.info(deviceInfo.toString());
        localDeviceServiceMap.get(deviceInfo.getDtuType()+"").deviceAdd(deviceInfo);
        return ResultHandler.ok(true);
    }

    @Override
    public Result deviceDelete(DeviceInfo deviceInfo) {
        log.info(deviceInfo.toString());
        localDeviceServiceMap.get(deviceInfo.getDtuType()+"").deviceDelete(deviceInfo);
        return ResultHandler.ok(true);
    }

    @Override
    public Result deviceUpdate(DeviceInfo deviceInfo) {
        log.info(deviceInfo.toString());
        localDeviceServiceMap.get(deviceInfo.getDtuType()+"").deviceUpdate(deviceInfo);
        return ResultHandler.ok(true);
    }

    @Override
    public Result cmd(@Valid DeviceCmdInfo baseDeviceCmd) {
        log.info(baseDeviceCmd.toString());
        localDeviceServiceMap.get(baseDeviceCmd.getDtuType()+"").cmd(baseDeviceCmd);
        return ResultHandler.ok(true);
    }
    @Data
    class LocalDeviceContrast {
        private String dtuType;
        private BaseLocalDeviceCollect BaseLocalDeviceCollect;

        public LocalDeviceContrast(String dtuType, BaseLocalDeviceCollect BaseLocalDeviceCollect) {
            this.dtuType = dtuType;
            this.BaseLocalDeviceCollect = BaseLocalDeviceCollect;
        }
    }
}
